package thread;

import java.util.Scanner;

public class Demo25 {
    public static void main(String[] args) {
        Object locker = new Object();
        Thread t1 = new Thread(() -> {
            synchronized (locker) {
                System.out.println("t1等待之前");
                try {
                    locker.wait();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                System.out.println("t1等待之后");
            }
        });

        Thread t2 = new Thread(() -> {
            Scanner scanner = new Scanner(System.in);
            synchronized (locker) {
                System.out.println("t2通知之前");
                //借助scanner控制阻塞，用户输入之前，都是阻塞状态
                scanner.next();
                locker.notify();//唤醒上述wait操作从而使t1能够回到RUNNABLE状态，并且参与调度
                System.out.println("t2通知之后");
                //打印顺序是固定的一定是先t2后t1 因为t1会从WAITING状态回到RUNNABLE状态然后进入BLOCKED状态
            }
        });

        t1.start();
        t2.start();
    }
}
